package com.jh.fcsm.service.basic.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.basic.BasicAreaCode;
import com.jh.fcsm.beans.basic.vo.BasicAreaCodeVo;
import com.jh.fcsm.common.BaseServiceImpl;
import com.jh.fcsm.common.exception.ServiceException;
import com.jh.fcsm.constant.Constant;
import com.jh.fcsm.dao.basic.BasicAreaCodeMapper;
import com.jh.fcsm.service.basic.BasicAreaCodeService;
import com.jh.fcsm.util.security.UUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  行政区划Service业务层处理
 * 
 * @author cjt
 * @date 2023-03-07
 */
@Service
@Transactional(readOnly = true)
public class BasicAreaCodeServiceImpl extends BaseServiceImpl<BasicAreaCodeMapper, BasicAreaCode> implements BasicAreaCodeService{
	
	private static final Logger logger = LoggerFactory.getLogger(BasicAreaCodeServiceImpl.class);
    @Autowired
    private BasicAreaCodeMapper basicAreaCodeMapper;

	@Override
	@Transactional(readOnly = false)
	/**
	 * 保存或更新行政区划
	 *@param basicAreaCode 行政区划对象
	 *@return String 行政区划ID
	 *@author szx
	 */
	public String saveOrUpdateBasicAreaCode(BasicAreaCode basicAreaCode) {
		if(basicAreaCode==null){
			throw new ServiceException("数据异常");
		}
		if(StringUtils.isEmpty(basicAreaCode.getId())){
			//新增
			basicAreaCode.setId(UUIDUtils.getUUID());
			basicAreaCodeMapper.insertSelective(basicAreaCode);
		}else{
			//避免页面传入修改
			basicAreaCode.setYn(null);
			basicAreaCodeMapper.updateByPrimaryKeySelective(basicAreaCode);
		}
		return basicAreaCode.getId();
	}

	@Override
	@Transactional(readOnly = false)
	/**
	 * 删除行政区划
	 *@param id void 行政区划ID
	 *@author szx
	 */
	public void deleteBasicAreaCode(List<String> ids) {
		for(String id:ids){
			//TODO 做判断后方能执行删除
			BasicAreaCode basicAreaCode=basicAreaCodeMapper.selectByPrimaryKey(id);
			if(basicAreaCode==null){
				throw new ServiceException("非法请求");
			}
			//逻辑删除
			BasicAreaCode tembasicAreaCode=new BasicAreaCode();
			tembasicAreaCode.setYn(Constant.NO);
			tembasicAreaCode.setId(basicAreaCode.getId());
			basicAreaCodeMapper.updateByPrimaryKeySelective(tembasicAreaCode);
		}
	}

	/**
	 * 查询行政区划详情
	 *@param id
	 *@return BasicAreaCode
	 *@author szx
	 */
    @Override
	public BasicAreaCode findById(String id) {
		return basicAreaCodeMapper.selectByPrimaryKey(id);
	}


	/**
	 * 分页查询行政区划
	 *@param basicAreaCodeVo
	 *@return PageInfo<BasicAreaCode>
	 *@author szx
	 */
	@Override
	public PageInfo<BasicAreaCode> findPageByQuery(BasicAreaCodeVo basicAreaCodeVo) {
		PageHelper.startPage(basicAreaCodeVo.getPageNum(),basicAreaCodeVo.getPageSize());
		Example example=new Example(BasicAreaCode.class);
		Criteria criteria=example.createCriteria();
		criteria.andEqualTo("yn",Constant.YES.byteValue());
//		查询条件
		if(!StringUtils.isEmpty(basicAreaCodeVo.getName())){
//			criteria.andEqualTo(basicAreaCodeVo.getName());
//			criteria.andEqualTo("name",basicAreaCodeVo.getName());
			criteria.andLike("name","%"+basicAreaCodeVo.getName()+"%");
		}
		if(!StringUtils.isEmpty(basicAreaCodeVo.getCode())){
//			criteria.andEqualTo(basicAreaCodeVo.getName());
			criteria.andLike("code","%"+basicAreaCodeVo.getCode()+"%");
		}
		if(!StringUtils.isEmpty(basicAreaCodeVo.getParentCode())){
//			criteria.andEqualTo(basicAreaCodeVo.getName());
			criteria.andLike("parentCode","%"+basicAreaCodeVo.getParentCode()+"%");
		}
		List<BasicAreaCode> basicAreaCodeList=basicAreaCodeMapper.selectByExample(example);
		Map<String,String> temp=mapper.selectAll().stream().collect(Collectors.toMap(BasicAreaCode::getCode, BasicAreaCode::getName,(key1, key2) -> key2));
		for (BasicAreaCode basicAreaCode : basicAreaCodeList) {
			if (basicAreaCode.getParentCode()!=null){
				basicAreaCode.setParentName(temp.get(basicAreaCode.getParentCode()));
			}
		}
		return new PageInfo<>(basicAreaCodeList);
	}

	@Override
	public List<BasicAreaCode> selectAll() {
		return basicAreaCodeMapper.selectAll();
	}


}
